package com.example.zhinongbao.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.zhinongbao.R;
import com.example.zhinongbao.activity.CartActivity;

import com.example.zhinongbao.activity.CollectionActivity;
import com.example.zhinongbao.activity.FootprintActivity;
import com.example.zhinongbao.activity.OrderManagementActivity;
import com.example.zhinongbao.activity.SettingActivity;


public class ProfileFragment extends Fragment {

    private TextView tvPhone, tvRealnameStatus;
    private ImageButton btnSettings;
    private LinearLayout layoutCart, layoutCollection, layoutFootprint;
    private LinearLayout layoutOrders, layoutPendingPayment, layoutPendingShipment;
    private LinearLayout layoutPendingReceipt, layoutPendingReview, layoutRefund;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_profile, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 初始化视图
        tvPhone = view.findViewById(R.id.tv_phone);
        tvRealnameStatus = view.findViewById(R.id.tv_realname_status);
        btnSettings = view.findViewById(R.id.btn_settings);

        layoutCart = view.findViewById(R.id.layout_cart);
        layoutCollection = view.findViewById(R.id.layout_collection);
        layoutFootprint = view.findViewById(R.id.layout_footprint);

        layoutOrders = view.findViewById(R.id.layout_orders);
        layoutPendingPayment = view.findViewById(R.id.layout_pending_payment);
        layoutPendingShipment = view.findViewById(R.id.layout_pending_shipment);
        layoutPendingReceipt = view.findViewById(R.id.layout_pending_receipt);
        layoutPendingReview = view.findViewById(R.id.layout_pending_review);
        layoutRefund = view.findViewById(R.id.layout_refund);

        // 设置点击监听器
        btnSettings.setOnClickListener(v -> openSettings());

        layoutCart.setOnClickListener(v -> openCart());
        layoutCollection.setOnClickListener(v -> openCollection());
        layoutFootprint.setOnClickListener(v -> openFootprint());

        layoutOrders.setOnClickListener(v -> openAllOrders());
        layoutPendingPayment.setOnClickListener(v -> openPendingPayment());
        layoutPendingShipment.setOnClickListener(v -> openPendingShipment());
        layoutPendingReceipt.setOnClickListener(v -> openPendingReceipt());
        layoutPendingReview.setOnClickListener(v -> openPendingReview());
        layoutRefund.setOnClickListener(v -> openRefund());
    }

    private void openSettings() {
       startActivity(new Intent(requireActivity(), SettingActivity.class));
    }

    private void openCart() {
        startActivity(new Intent(requireActivity(), CartActivity.class));
    }

    private void openCollection() {
      startActivity(new Intent(requireActivity(), CollectionActivity.class));
    }

    private void openFootprint() {
      startActivity(new Intent(requireActivity(), FootprintActivity.class));
    }

    private void openAllOrders() {
        Intent intent = new Intent(requireActivity(), OrderManagementActivity.class);
        intent.putExtra("tab_index", 0); // 全部订单
        startActivity(intent);
    }

    private void openPendingPayment() {
        Intent intent = new Intent(requireActivity(), OrderManagementActivity.class);
        intent.putExtra("tab_index", 1); // 待付款
        startActivity(intent);
    }

    private void openPendingShipment() {
        Intent intent = new Intent(requireActivity(), OrderManagementActivity.class);
        intent.putExtra("tab_index", 2); // 待发货
        startActivity(intent);
    }

    private void openPendingReceipt() {
        Intent intent = new Intent(requireActivity(), OrderManagementActivity.class);
        intent.putExtra("tab_index", 3); // 待收货
        startActivity(intent);
    }

    private void openPendingReview() {
        Intent intent = new Intent(requireActivity(), OrderManagementActivity.class);
        intent.putExtra("tab_index", 4); // 待评价
        startActivity(intent);
    }

    private void openRefund() {
        Intent intent = new Intent(requireActivity(), OrderManagementActivity.class);
        intent.putExtra("tab_index", 5); // 退款/售后
        startActivity(intent);
    }
}